package design.singleDesign;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SpringSingleDesign {
    private SpringSingleDesign(){};

    private static Map<String,Object> map=new ConcurrentHashMap<String,Object>();

    public static Object getBeans(String ClassName) {
        synchronized(map) {
            //如果从未实例化
            if (!map.containsKey(ClassName)) {
                //则进行实例化
                Object obj = null;
                try {
                    obj = Class.forName(ClassName).newInstance();
                    map.put(ClassName, obj);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return obj;
            }
            return map.get(ClassName);
        }
    }
}
